import time, datetime

#datetime. datetime 转换成 str
t =  datetime. datetime. now(tz = None)
print(t.strftime("%Y-%m-%d %H:%M:%S"))

# str 转换成 datetime. datetime
t = datetime. datetime. now(tz = None)
a = t.strftime("%Y-%m-%d %H:%M:%S")
b = datetime. datetime. strptime(a, "%Y-%m-%d %H:%M:%S")

#datetime.datetime 转换成 timestamp
t = datetime. datetime. now(tz = None)
c = t. timestamp()
print(c)

#timestamp 转换成 datetime. datetime
d = datetime.datetime.fromtimestamp(c, tz = None)
print(d)

#timestamp 转换成 str
e = datetime.datetime.fromtimestamp(c, tz = None)
f = e.strftime("%Y-%m-%d %H:%M:%S")
print(f)

# str 转换成 timestamp
g = datetime.datetime.strptime(f, "%Y-%m-%d %H:%M:%S")
h = g.timestamp()
print(h)

#封闭一个函数，如果传入的是-1，输出的就是字符串时间
import datetime
def get_date():
    n = input("若想知道当前日期，请输入-1：")
    if n == "-1":
        t = datetime.datetime.now(tz=None)
        print(t.strftime("%Y-%m-%d"))
    else:
        print("您输入的不是-1，请重新输入！")
        get_date()
    return

get_date()

